package com.yanqu.road.logic.activity.hotpotjump.game;

import java.util.ArrayList;
import java.util.List;

public class JumpBoardGrid {
    private int x;
    private int y;
    private int q;
    private int r;

    private String printf="";

    public JumpBoardGrid() {
    }

    public JumpBoardGrid(int x, int y) {
        this.x = x;
        this.y = y;

    }

    public int getX() {
        return x;
    }

//    public void setX(int x) {
//        this.x = x;
//    }

    public int getY() {
        return y;
    }

//    public void setY(int y) {
//        this.y = y;
//    }

    public String getPrintf() {
        return printf;
    }

    public void setPrintf(String printf) {
        this.printf = printf;
    }

    public int getQ() {
        return q;
    }

    public void setQ(int q) {
        this.q = q;
    }

    public int getR() {
        return r;
    }

    public void setR(int r) {
        this.r = r;
    }

    /**
     * 和其他点的距离
     * @param other
     * @return
     */
    public int distanceTo(JumpBoardGrid other) {
        return (Math.abs(this.getCubeX() - other.getCubeX())
                + Math.abs(this.getCubeY() - other.getCubeY())
                + Math.abs(this.getCubeZ() - other.getCubeZ())) / 2;
    }

    /**
     * 立方坐标中的x坐标
     * @return
     */
    public int getCubeX() {
        return q;
    }

    /**
     * 立方坐标中的y坐标
     * @return
     */
    public int getCubeY() {
        return r;
    }

    /**
     * 立方坐标中的z坐标
     * @return
     */
    public int getCubeZ() {
        return -q - r;
    }

    /**
     * 是否相邻
     * @param neighbor
     * @return
     */
    public boolean isNeighbor(JumpBoardGrid neighbor) {
        return distanceTo(neighbor) == 1;
    }

    /**
     * 获得指定点在自己的哪个方向
     */
    public eJumpDirection calcTargetDirection(JumpBoardGrid target) {
        int dx = target.getCubeX() - this.getCubeX();
        int dy = target.getCubeY() - this.getCubeY();
        int dz = target.getCubeZ() - this.getCubeZ();

        if (dx > 0 && dy < 0 && dz == 0) return eJumpDirection.RightUp;
        if (dx > 0 && dy == 0 && dz < 0) return eJumpDirection.Right;
        if (dx == 0 && dy > 0 && dz < 0) return eJumpDirection.RightDown;
        if (dx < 0 && dy > 0 && dz == 0) return eJumpDirection.LeftDown;
        if (dx < 0 && dy == 0 && dz > 0) return eJumpDirection.Left;
        if (dx == 0 && dy < 0 && dz > 0) return eJumpDirection.LeftUp;

        return eJumpDirection.None;
    }

    /**
     * 找到指定方向上最近的一个x,y坐标
     * @return
     */
    public List<Integer> findDirectionNeighborPos(eJumpDirection direction) {
        List<Integer> pos = new ArrayList<>();
        int findY = getY();
        int findX = getX();
        switch (direction) {
            case Left: {
                findY = y;
                findX = x - 1;
            }
            break;
            case Right: {
                findY = y;
                findX = x + 1;
            }
            break;
            case LeftUp: {
                if (y % 2 == 0) {
                    findY = y - 1;
                    findX = x;
                } else {
                    findY = y - 1;
                    findX = x - 1;
                }
            }
            break;
            case RightUp: {
                if (y % 2 == 0) {
                    findY = y - 1;
                    findX = x + 1;
                } else {
                    findY = y - 1;
                    findX = x;
                }
            }
            break;
            case LeftDown: {
                if (y % 2 == 0) {
                    findY = y + 1;
                    findX = x;
                } else {
                    findY = y + 1;
                    findX = x - 1;
                }
            }
            break;
            case RightDown: {
                if (y % 2 == 0) {
                    findY = y + 1;
                    findX = x + 1;
                } else {
                    findY = y + 1;
                    findX = x;
                }
            }
            break;
            default:
                break;
        }
        pos.add(findX);
        pos.add(findY);
        return pos;
    }

    public List<List<Integer>> getNeighborPos() {
        List<List<Integer>> posList = new ArrayList<>();
        for (eJumpDirection direction : eJumpDirection.values()) {
            List<Integer> pos = findDirectionNeighborPos(direction);
            posList.add(pos);
        }
        return posList;
    }

    public Integer getPosKey() {
        return x * 1000 + y;
    }
}
